home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5760 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: String Arrays and string parsing
  5. Date: 20 Feb 96 22:36:13 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.824855773@rscernix>
  8. References: <31287278.789445@news.inforamp.net> <4gc7qc$dm7@maureen.teleport.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4gc7qc$dm7@maureen.teleport.com> GHouck <hksys@teleport.com> writes:
  13.  
  14. >dsheuman@inforamp.net (Danny Heuman) wrote:
  15. >>I have day, month, and year as DDMMMYYYY and would like to parse it
  16. >>out in to DD, MMM, YYYY.  I can parse the DD out by using
  17. >>strncpy(into1, from1, 2).  I can parse the YYYY out by doing
  18. >>strcpy(into3, from1 + 5).  How can I parse out the MMM?  Can I use
  19. >>either of these functions that work above, strncpy or strcpy?  If so,
  20. >>once parsed, do I have to attach an '\0' to the end of it to keep it
  21. >>as a character string?
  22. >>
  23. >Yes, you can use 'strncpy'. Yes, you have to add zero. Try the following:
  24. >
  25. >  strncpy( month,(datestring+2),3 ); /* copy MMM from interior */
  26. >  month[3] = '\000';                 /* add null terminator */
  27.  
  28. Or you can parse everything with a single sscanf call:
  29.  
  30. sscanf(from1, "%2s%3s%4s", into1, into2, into3);
  31.  
  32. sscanf will null-terminate all three strings for you.
  33.  
  34. If some of your characters can be spaces (e.g. " 2JAN1988"), you have
  35. to replace %Ns by %N[^\n] (that is, assuming that no embedded newlines
  36. are allowed :-)
  37.  
  38. Dan
  39. --
  40. Dan Pop
  41. CERN, CN Division
  42. Email: danpop@mail.cern.ch 
  43. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  44.